package com.doge.calcalbum.business.calc

import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.*
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
import com.doge.calcalbum.R
import com.doge.calcalbum.business.PolicyViewModel
import com.doge.calcalbum.business.WebActivity
import com.doge.calcalbum.core.BaseDialogFragment
import com.doge.calcalbum.databinding.DialogFragmentPolicyBinding
import com.doge.calcalbum.utils.*

class PolicyDialogFragment : BaseDialogFragment() {
    private var innerBinding: DialogFragmentPolicyBinding? = null
    private val binding get() = innerBinding!!
    override val hasShadow = true
    override val cancelable = false
    private val viewModel by activityViewModels<PolicyViewModel>()

    override fun setWindow(win: Window) {
        win.decorView.setPadding(38.dp, 0, 38.dp, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        win.attributes = lp
        win.setGravity(Gravity.CENTER)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding =
            innerBinding ?: DialogFragmentPolicyBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val flag1 = getString(R.string.policy_flag1) // 用户协议
        val flag2 = getString(R.string.policy_flag2) // 隐私政策
        val desc = getString(R.string.policy_desc)
        val agreement = SpannableString(desc)
        val indexFlag1 = desc.indexOf(flag1)
        val indexFlag2 = desc.indexOf(flag2)
        agreement.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                WebActivity.start(widget.context, urlUserPolicy)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = (0xFF3369FF).toInt()
                ds.isUnderlineText = false
            }

        }, indexFlag1, indexFlag1 + flag1.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        agreement.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                WebActivity.start(widget.context, urlPrivacy)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = (0xFF3369FF).toInt()
                ds.isUnderlineText = false
            }

        }, indexFlag2, indexFlag2 + flag2.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.message.text = agreement
        binding.message.movementMethod = LinkMovementMethod.getInstance()
        binding.message.highlightColor = Color.TRANSPARENT


        binding.cancel.setOnClickCallback { Activities.finishAll() }
        binding.confirm.setOnClickCallback { agree() }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.cancel.setOnClickListener(null)
        binding.confirm.setOnClickListener(null)
    }


    private fun agree() {
        viewModel.agree()
        spWrite(key, true, folder)
        dismissAllowingStateLoss()
    }

    companion object {
        private const val key = "d"
        private const val folder = "policyBehavior"
        private val hasDisplayed: Boolean get() = spReadBoolean(key, false, folder)

        fun tryShowPolicyDialogInFirstTime(context: FragmentActivity) {
            if (!hasDisplayed) {
                newInstance().show(context.supportFragmentManager)
            }
        }

        private fun newInstance(): PolicyDialogFragment {
            return PolicyDialogFragment()
        }
    }
}